<?php
/**
 *10开头异常 用户异常
 *20开头异常 资金异常
 *30开头异常 订单异常
 *40开头异常 房车异常
 *50开头异常 日历异常
 *60开头异常 心愿异常
 *70开头异常 提现异常
 */
namespace app\common\exception;

use think\exception\Handle;
use think\Request;
use think\exception\HttpException;
use think\exception\ErrorException;
use RuntimeException;
use think\Exception;
use think\Lang;
use think\Config;

class Http extends Handle
{
    public function render(\Exception $e)
    {
        if ($e instanceof Exception) {

            $code    = $e->getCode();
            $message = $e->getMessage();
            $type    = substr($code, 0, 2);

            switch ($type) {
                case '10':
                    return $this->translate(['type' => 'member', 'code' => $code, 'message' => $message]); 
                    break;
                case '20':
                    return $this->translate(['type' => 'capital', 'code' => $code, 'message' => $message]); 
                    break;
                case '30':
                    return $this->translate(['type' => 'order', 'code' => $code, 'message' => $message]); 
                    break;
                case '40':
                    return $this->translate(['type' => 'caravan', 'code' => $code, 'message' => $message]); 
                    break;
                case '50':
                    return $this->translate(['type' => 'calendar', 'code' => $code, 'message' => $message]); 
                    break;
                case '60':
                    return $this->translate(['type' => 'wish', 'code' => $code, 'message' => $message]); 
                    break;
                case '70':
                    return $this->translate(['type' => 'cash', 'code' => $code, 'message' => $message]); 
                    break;
                default:
                    return $this->translate(['type' => 'base', 'code' => $code, 'message' => $message]);
                    break;
            }
            
        }elseif (1) {
            return parent::render($e);
        }
        
    }

    public function translate($info)
    {
        if (Request::instance()->isAjax() || Config::get('default_return_type') == 'json') {
            return json($info);
        }else {
            $info['type'] = 'base';
            $url = 'javascript:history.back(-1);';
            return view('common@exception/' . $info['type'], array_merge($info, ['wait' => 100, 'url' => $url]));
        }
    }

}